package com.hongxun.budget.ui.activity.sg;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.utils.DateUtils;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.greenentity.sg.CArrivalRecordEntity;
import com.hongxun.greenentity.sg.CGoodsDetailEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 到货记录详情
 */
public class MaterialArriveRecordListActivity extends BaseListActivity {
    @BindView(R.id.tv3_proname)
    TextView tv3DfdName;

    private SuperAdapter superAdapter;
    private List<CGoodsDetailEntity> entityList = new ArrayList<>();
    private Map<String, Date> dateMap = new HashMap<>();

    @Override
    public int getMyListLayoutId() {
        return R.layout.sg_activity_material_arrive_record_list;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        tv3DfdName.setText(getIntent().getStringExtra("pointName"));
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public SuperAdapter getAdapter() {
        superAdapter = new SuperAdapter<CGoodsDetailEntity>(R.layout.sg_content_cp_mrd_adapter) {
            @Override
            public void bindView(ViewHolder viewHolder, CGoodsDetailEntity itemBean, int i) {
                try {
                    ((TextView) viewHolder.getView(R.id.tv1)).setText(itemBean.getGDName());
                    ((TextView) viewHolder.getView(R.id.tv2)).setText(itemBean.getGDBigtype() + "");
                    ((TextView) viewHolder.getView(R.id.tv3)).setText(itemBean.getGDNum() + "");
                    ((TextView) viewHolder.getView(R.id.tv4)).setText(DateUtils.LongToStringyyMMdd_(dateMap.get(itemBean.getARGuid()).getTime()));
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setData(entityList);
        superAdapter.setEmptyDataView(R.layout.view_empty);
        return superAdapter;
    }

    @Override
    public void onRefresh() {
        dateMap.clear();
        entityList.clear();
        Observable.just(SgStrKey.uuiddfd).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String value) {
                String dfdid = String.valueOf(SPUtils.get(context, value, ""));
                List<CArrivalRecordEntity> list = SgDBUtils.getCArListAll(dfdid);
                for (CArrivalRecordEntity entity : list) {
                    dateMap.put(entity.getGuid(), entity.getARTime());
                    entityList.addAll(SgDBUtils.getCGdListAll(entity.getGuid()));
                }
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean b) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        onRefresh();
    }
}
